package beautiful.butterfly.drds.data_exchange.data_exchange.record.column;

import beautiful.butterfly.drds.data_exchange.error_code.CommonErrorCode;
import beautiful.butterfly.drds.data_exchange.error_code.DataExchangeException;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

public class LongColumn extends Column
{

    /**
     * 从整形字符串表示转为LongColumn，支持Java科学计数法
     * <p>
     * NOTE: <br>
     * 如果data为浮点类型的字符串表示，数据将会失真，请使用DoubleColumn对接浮点字符串
     */
    public LongColumn(final String string)
    {
        super(null, ColumnType.LONG, 0);
        if (null == string)
        {
            return;
        }

        try
        {
            BigInteger bigInteger = NumberUtils.createBigDecimal(string).toBigInteger();
            //底层的类型BigInteger
            super.setObject(bigInteger);

            // 当 rawData 为[0-127]时，rawData.bitLength() < 8，导致其 byteSize = 0，简单起见，直接认为其长度为 data.length()
            // super.setByteSize(rawData.bitLength() / 8);
            super.setByteSize(string.length());
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("String[%s]不能转为Long .", string));
        }
    }

    public LongColumn(Long aLong)
    {
        this(null == aLong ? (BigInteger) null : BigInteger.valueOf(aLong));
    }

    public LongColumn(Integer integer)
    {
        this(null == integer ? (BigInteger) null : BigInteger.valueOf(integer));
    }

    public LongColumn(BigInteger bigInteger)
    {
        this(bigInteger, null == bigInteger ? 0 : 8);
    }

    private LongColumn(BigInteger bigInteger, int byteSize)
    {
        super(bigInteger, ColumnType.LONG, byteSize);
    }

    public LongColumn()
    {
        this((BigInteger) null);
    }

    //
    @Override
    public Boolean asBoolean()
    {
        if (null == this.getObject())
        {
            return null;
        }

        return this.asBigInteger().compareTo(BigInteger.ZERO) != 0 ? true
                : false;
    }


    @Override
    public Long asLong()
    {
        BigInteger bigInteger = (BigInteger) this.getObject();
        if (null == bigInteger)
        {
            return null;
        }

        OverFlowChecker.validateDataIsNotOverFlowLongRange(bigInteger);

        return bigInteger.longValue();
    }

    @Override
    public Double asDouble()
    {
        if (null == this.getObject())
        {
            return null;
        }

        BigDecimal bigDecimal = this.asBigDecimal();
        OverFlowChecker.validateDataIsNotOverFlowDoubleRange(bigDecimal);

        return bigDecimal.doubleValue();
    }


    @Override
    public BigDecimal asBigDecimal()
    {
        if (null == this.getObject())
        {
            return null;
        }

        return new BigDecimal(this.asBigInteger());
    }

    @Override
    public BigInteger asBigInteger()
    {
        if (null == this.getObject())
        {
            return null;
        }

        return (BigInteger) this.getObject();
    }

    @Override
    public String asString()
    {
        if (null == this.getObject())
        {
            return null;
        }
        return ((BigInteger) this.getObject()).toString();
    }

    @Override
    public Date asDate()
    {
        if (null == this.getObject())
        {
            return null;
        }
        return new Date(this.asLong());
    }

    @Override
    public byte[] asBytes()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Long类型不能转为Bytes .");
    }

}
